//
//  PowerBelongViewController.m
//  GetPowerDBO
//
//  Created by 周博 on 2017/6/23.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "PowerBelongViewController.h"
#import "PowerBelongTableViewCell.h"
#import "GetQRCodeViewController.h"
#import "UIImage+GIF.h"

#define kCellName @"PowerBelongTableViewCell"

@interface PowerBelongViewController ()<UITableViewDelegate,UITableViewDataSource,PowerBelongTableViewCellDelegate>
{
    NSInteger _pageIndex;

}
@property (nonatomic,strong) NSMutableArray  *dataArray;
@end

@implementation PowerBelongViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _dataArray = [NSMutableArray array];
    [self UISetting];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self loadData];

}


- (void)UISetting{
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        _pageIndex = 0;
        
        [self loadData];
        
        [_tableView.mj_header endRefreshing];
    }];
    
    _tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        
        _pageIndex ++;
        
        [self loadData];
        
        [_tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - 数据下载

- (void)loadData{
    
    [self.hub hideAnimated:YES];
    
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    // Set the label text.
    self.hub.label.text = NSLocalizedString(@"加载中...", @"HUD loading title");
    
    NSString *pageIndexString = [NSString stringWithFormat:@"%ld",_pageIndex];
    
    [[AFClient shareInstance] powerBelongByShopId:_model.id eid:kUserId page_size:@"20" page_index:pageIndexString progressBlock:nil success:^(id responseBody) {
        [self.hub hideAnimated:YES];
        if ([responseBody[@"code"] integerValue] == 200) {
            
            if (_pageIndex == 0) {
                [_dataArray removeAllObjects];
            }
            
            for (NSDictionary *dic in responseBody[@"data"]) {
                PowerModel *model = [[PowerModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_dataArray addObject:model];
            }
            [self creatImageBackView:_dataArray];
            [_tableView reloadData];
        }else{
            kAlert(responseBody[@"message"]);
        }
    } failure:^(NSError *error) {
        [self.hub hideAnimated:YES];
    }];
}

#pragma mark - 点击事件
- (IBAction)backButtonClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark - tableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PowerBelongTableViewCell *cell = (PowerBelongTableViewCell *)[tableView dequeueReusableCellWithIdentifier:kCellName];
    cell.delegate = self;
    cell.labelNum.text = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
    
    PowerModel *model = _dataArray[indexPath.row];
    
    [cell showDataWithModel:model];
    return cell;
}

#pragma cellDelegate

- (void)clickPowerInfoAboutResaultCode:(NSString *)ResaultCode alias:(NSString *)alias{
//    kAlert(alias);
    UIStoryboard *storyboard = kMainStroyboard;
    GetQRCodeViewController *qrCodeVC = [storyboard instantiateViewControllerWithIdentifier:@"GetQRCodeViewController"];
    qrCodeVC.model = _model;
    qrCodeVC.alias = alias;
    qrCodeVC.resaultCode = ResaultCode;
    qrCodeVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [self presentViewController:qrCodeVC animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
